#include "Gameplay.h"

Gameplay::Gameplay()
{
    screenName = "Gameplay";

    zA = 0.0f;
    rotateX = rotateY = 0.0f;
    speedX = speedY = 0.0f;

    // Load texture
    cubeTexture = ContentManager::LoadContent<Texture2D>("Media/crate.bmp");

    // Set up lighting
    lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f }; // Alpha param does nothing
    lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
    lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };
    // Setup ambient light
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
    // Setup diffuse light
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
    // Position light
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    // Enable light
    glEnable(GL_LIGHT0);

    // Display list setup:
    cubeDispList = glGenLists(1);
    glNewList(cubeDispList, GL_COMPILE);
        test3D();
    glEndList();
    dispList = false; // Turn off use of display lists for now

	camera.setCenter(x, y, z); // Set center to origin
    camera.setDistance(12.0f);
    Engine::Instance().currentCamera = &camera;
    addChild(&camera);
    /*
    // Initial camera position
    camera.setEye(0.0f, 0.0f, 5.0f);
    // Pass reference to engine
    Engine::Instance().currentCamera = &camera;
    // Add camera to scene graph to receive events/updates
    addChild(&camera);
    */
}

void Gameplay::draw()
{
    glTranslatef(0.0f, 0.0f, zA);
    // Rotate along x and y
	glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
	glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

    // Draw cube
    if (dispList)
        glCallList(cubeDispList);
    else
        test3D();
}

void Gameplay::update(Uint32* deltaTime)
{
	rotateX += speedX;
	rotateY += speedY;
}

void Gameplay::handleEvents(SDL_Event* event)
{
    // Note: Handling key events this way counts them twice:
    // once when pressed, another when released
    if (event->type == SDL_KEYDOWN)
    {
        if(event->key.keysym.sym == SDLK_UP)
            speedX -= 0.02f;
        else if(event->key.keysym.sym == SDLK_DOWN)
            speedX += 0.02f;
        else if(event->key.keysym.sym == SDLK_LEFT)
            speedY -= 0.02f;
        else if(event->key.keysym.sym == SDLK_RIGHT)
            speedY += 0.02f;
        else if(event->key.keysym.sym == SDLK_PAGEUP)
            zA -= 0.2f;
        else if(event->key.keysym.sym == SDLK_PAGEDOWN)
            zA += 0.2f;
        else if(event->key.keysym.sym == SDLK_l)
        {
            if(!glIsEnabled(GL_LIGHTING))
            {
                glEnable(GL_LIGHTING);
            }
            else
            {
                glDisable(GL_LIGHTING);
            }
        }
        else if(event->key.keysym.sym == SDLK_b)
        {
            if(!glIsEnabled(GL_BLEND))
            {
                glEnable(GL_BLEND);
                glDisable(GL_DEPTH_TEST);
                glBlendFunc(GL_SRC_ALPHA,GL_ONE);
            }
            else
            {
                glDisable(GL_BLEND);
                glEnable(GL_DEPTH_TEST);
            }
        }
        else if(event->key.keysym.sym == SDLK_d)
        {
            // Toggle display list for cube
            //dispList = !dispList;
            if (dispList)
            {
                dispList = false;
                std::cout << "Display list with cube is off." << std::endl;
            }
            else
            {
                dispList = true;
                std::cout << "Display list with cube is on." << std::endl;
            }
        }
        // Move back to main screen
        else if (event->key.keysym.sym == SDLK_ESCAPE)
        {
            // Create and add new screen
            MainMenu* menu = new MainMenu();
            Engine::Instance().addScreen2D(menu);
            // Turn this screen off
            visible = updatable = eventable = false;
        }
    }
}

void Gameplay::test3D()
{
    // Bind the texture we loaded to the cube.
    glBindTexture(GL_TEXTURE_2D, cubeTexture->ID);
    /* Start Drawing Quads */
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Wireframe
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    glBegin(GL_QUADS);
        /* Front Face */
        //glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
        /* Normal Pointing Towards Viewer */
        glNormal3f( 0.0f, 0.0f, 1.0f );
        /* Point 1 (Front) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f,  1.0f );
        /* Point 2 (Front) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  1.0f, -1.0f,  1.0f );
        /* Point 3 (Front) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f(  1.0f,  1.0f,  1.0f );
        /* Point 4 (Front) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f,  1.0f,  1.0f );

        /* Back Face */
        //glColor4f(0.0f, 1.0f, 1.0f, 1.0f);
        /* Normal Pointing Away From Viewer */
        glNormal3f( 0.0f, 0.0f, -1.0f);
        /* Point 1 (Back) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
        /* Point 2 (Back) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
        /* Point 3 (Back) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  1.0f,  1.0f, -1.0f );
        /* Point 4 (Back) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f, -1.0f, -1.0f );

        /* Top Face */
        //glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
        /* Normal Pointing Up */
        glNormal3f( 0.0f, 1.0f, 0.0f );
        /* Point 1 (Top) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
        /* Point 2 (Top) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f,  1.0f,  1.0f );
        /* Point 3 (Top) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  1.0f,  1.0f,  1.0f );
        /* Point 4 (Top) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f(  1.0f,  1.0f, -1.0f );

        /* Bottom Face */
        //glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        /* Normal Pointing Down */
        glNormal3f( 0.0f, -1.0f, 0.0f );
        /* Point 1 (Bottom) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
        /* Point 2 (Bottom) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  1.0f, -1.0f, -1.0f );
        /* Point 3 (Bottom) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f, -1.0f,  1.0f );
        /* Point 4 (Bottom) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f,  1.0f );

        /* Right face */
        //glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
        /* Normal Pointing Right */
        glNormal3f( 1.0f, 0.0f, 0.0f);
        /* Point 1 (Right) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, -1.0f );
        /* Point 2 (Right) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f,  1.0f, -1.0f );
        /* Point 3 (Right) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f,  1.0f,  1.0f );
        /* Point 4 (Right) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f,  1.0f );

        /* Left Face*/
        //glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
        /* Normal Pointing Left */
        glNormal3f( -1.0f, 0.0f, 0.0f );
        /* Point 1 (Left) */
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
        /* Point 2 (Left) */
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f,  1.0f );
        /* Point 3 (Left) */
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f,  1.0f,  1.0f );
        /* Point 4 (Left) */
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
    glEnd();
}
